package com.kamistoat.meimeimall.meimeimallseckill.dubbo;

import com.alibaba.fastjson.JSON;
import com.kamistoat.meimeimall.common.To.seckillTo.SecKillProductCacheTo;
import com.kamistoat.meimeimall.common.To.seckillTo.SecKillSessionTo;
import com.kamistoat.meimeimall.common.annotation.DubboAndHTTPExceptionCatch;
import com.kamistoat.meimeimall.common.dubboInterface.SecKillDubboInterface;
import com.kamistoat.meimeimall.common.utils.R;
import com.kamistoat.meimeimall.meimeimallseckill.service.SeckillSessionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Service
@Component
public class SecKillDubboInterfaceImpl implements SecKillDubboInterface {
    @Autowired
    SeckillSessionService seckillSessionService;

    /**
     * 删除redis中已经缓存的原秒杀.
     * 并重新检查相同id是否是3天以内的秒杀，如果是，立刻上架.
     *
     * @param redisSessionKeyList
     * @return
     */
    @Override
    @DubboAndHTTPExceptionCatch
    public R deleteAndReUpToRedis(List<String> redisSessionKeyList) {
        seckillSessionService.terminateSessions(redisSessionKeyList);
        return R.ok();
    }

    /**
     * 查询未来所有秒杀活动的商品信息
     *
     * @return
     */
    @Override
    @DubboAndHTTPExceptionCatch
    public R getFutureSecKillSession() {
        List<SecKillSessionTo> futureSecKillSessionList = seckillSessionService.getFutureSecKillSessionList();
        return R.ok().put("futureSecKillSessionList", JSON.toJSONString(futureSecKillSessionList));
    }

    /**
     * 查询一个商品在当前所处的秒杀活动。
     * 如果当前为普通状态，则返回未来三天内的下一场秒杀活动
     *
     * @param skuId
     * @return
     */
    @Override
    @DubboAndHTTPExceptionCatch
    public R getSecKillInfoThreeDays(Long skuId) {
        SecKillProductCacheTo secKillInfoThreeDays = seckillSessionService.getSkuInSession(skuId);
        return R.ok().put("secKillInfoThreeDays", JSON.toJSONString(secKillInfoThreeDays));
    }


}
